package ro.ubb.dataaccess.service.impl;

import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;

import ro.ubb.dataaccess.entities.Author;
import ro.ubb.dataaccess.exception.MyBbException;
import ro.ubb.dataaccess.service.DataAccessServiceAuthor;

@Stateless
public class DataAccessServiceAuthorEJB extends DataAccessServiceBase<Author> implements DataAccessServiceAuthor {

	private static final Class<Author> type = Author.class;

	protected DataAccessServiceAuthorEJB() {
		super(type);
	}

	public Author getByName(String name) throws MyBbException {
		TypedQuery<Author> query = em.createNamedQuery("Author.findByName", type);
		query.setParameter("name", like(name));
		try {
			return query.getSingleResult();
		} catch (NoResultException nre) {
			throw new MyBbException("No result!", nre.getCause());
		} catch (NonUniqueResultException nue) {
			throw new MyBbException("Not unique result!", nue.getCause());
		} catch (PersistenceException pe) {
			throw new MyBbException("General persistence exception", pe.getCause());
		}

	}
}
